/**
 * Outil pour la gestion de la suppression du mot de passe
 * dans le module de connection rapide sur la droite
 */
var isOkpassword = false;
var isOklogin = false;
function load(a,b) {
   if (a == "idUser" && isOklogin == false) {
      b.value = '';
      isOklogin = true;
   } else if (isOkpassword == false) {
      b.value = '';
      isOkpassword = true;
   }
}


function confirmSuppression(message){if(confirm(message)){return true;}else{return false;}}

/**
 * Gestion du focus sur les inputs
 * Notamment pour les couleurs
 */
function inputFocus(event){this.style.backgroundColor = '#FC0';}
function inputNoFocus(event){this.style.backgroundColor = '#FFFFFF'; }

/**
 * Création d'un objet XHR en vue d'une architecture AJAX
 */
function initXHR()
{
		xhr_object = null;
   		if (window.ActiveXObject) {
			try {
				xhr_object = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
					return null;
				}
    	   	}
        }
		else if (window.XMLHttpRequest) {
			xhr_object = new XMLHttpRequest();
			if (xhr_object.overrideMimeType) { xhr_object.overrideMimeType("text/xml"); }
 		}
		return xhr_object;
}

var oldResp = null;


function check(e) {
	value = this.id;
	text = this.value;
	xhr_object = initXHR();
	
	if (text.length == 0) {
		oldResp = null;
		document.getElementById("info" + value).innerHTML = '';
		return;
	}
	else {
		if (value == "mailUser") {
			var reg = new RegExp("^[a-z0-9._-]+@[a-z0-9._-]{2,}.[a-z]{2,4}$");
			if (reg.exec(text) == null) {
				valid(value,false,'Format invalide')
				return;
			} else {
                valid(value,true,'Format valide');
                return;
            }
		}

		if (value == "ageUser") {
			var reg = new RegExp("^[1-9][0-9]$");
			if (reg.exec(text) == null) {
				valid(value,false,'Entrez un nombre !')
				return;
			} else {
                valid(value,true,'Âge valide');
                return;
            }
		}
		
		if (value == "passUser") {
			if (text.length<6) {
				valid(value,false,'Le password doit faire au moins 6 caractères !')
				return;
			} else {
                valid(value,true,'password valide');
                return;
            }
		}
		
		if (value == "Header") {
			if (text.length<1) {
				valid(value,false,'Ecrire un sujet')
				return;
			} else {
                valid(value,true,'Header Valide');
                return;
            }
		}
		
		if(xhr_object && xhr_object.readyState != 0)
		{
			xhr_object.abort();
			delete xhr_object;
		}

		xhr_object = initXHR();
		xhr_object.open("POST", "VerificationAJAX", true);
		xhr_object.onreadystatechange = function() {
		   if(xhr_object.readyState == 4 && xhr_object.status == 200) {
				var resp = xhr_object.responseText;
				if (value + resp != oldResp) {
					document.getElementById("info" + value).innerHTML = resp;
					oldResp = value + resp;
				}
			}
		}
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr_object.send(value + "="+document.getElementById(value).value);
	}
	return;
}

function valid(value,valid,message)
{
	var resp = (valid != "neutre" ? '<img src="' + (valid ? './images/valid.gif' : './images/invalid.gif') + '"/><i>&nbsp;' + message + '</i>' : '');
	if (value + resp != oldResp) {
		document.getElementById("info" + value).innerHTML = resp;
		oldResp = value + resp;
	}
}

function init()
{

	if (document.forms["addUser"] != null)
	{
		for(i=0; i < document.forms["addUser"].getElementsByTagName("input").length;i++) {
			document.forms["addUser"].getElementsByTagName("input")[i].onkeyup = check;
		}
	}
	

	if (document.forms["SendMessage"] != null)
	{
		for(i=0; i < document.forms["SendMessage"].getElementsByTagName("input").length;i++) {
			document.forms["SendMessage"].getElementsByTagName("input")[i].onkeyup = check;
		}
	}

}

window.onload = init;